package org.example.project.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.TextFieldDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import kotlinx.datetime.LocalDate

/**
 * 年月选择器组件
 */
@Composable
fun MonthYearPicker(
    selectedDate: LocalDate,
    onDateSelected: (LocalDate) -> Unit,
    modifier: Modifier = Modifier
) {
    var currentYear by remember { mutableStateOf(selectedDate.year) }
    var currentMonth by remember { mutableStateOf(selectedDate.monthNumber) }
    var showPicker by remember { mutableStateOf(false) }

    val monthNames = listOf(
        "一月", "二月", "三月", "四月", "五月", "六月",
        "七月", "八月", "九月", "十月", "十一月", "十二月"
    )

    Column(modifier = modifier) {
        // 年月显示和选择器触发按钮
        OutlinedTextField(
            value = "${currentYear}年${currentMonth}月",
            onValueChange = { },
            label = { Text("选择年月") },
            readOnly = true,
            modifier = Modifier.fillMaxWidth().clickable { showPicker = true },
            trailingIcon = {
                IconButton(onClick = { showPicker = true }) {
                    Icon(Icons.Default.ArrowForward, contentDescription = "选择年月")
                }
            },
            colors = TextFieldDefaults.outlinedTextFieldColors(
                focusedBorderColor = MaterialTheme.colors.primary,
                unfocusedBorderColor = MaterialTheme.colors.primary.copy(alpha = 0.5f),
                cursorColor = MaterialTheme.colors.primary
            )
        )

        // 年月选择弹出框
        if (showPicker) {
            Dialog(onDismissRequest = { showPicker = false }) {
                Surface(
                    modifier = Modifier.fillMaxWidth().padding(16.dp),
                    shape = RoundedCornerShape(8.dp),
                    color = MaterialTheme.colors.surface,
                    elevation = 8.dp
                ) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        // 年份选择
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            IconButton(onClick = { currentYear-- }) {
                                Icon(Icons.Default.ArrowBack, contentDescription = "上一年")
                            }

                            Text(
                                "${currentYear}年",
                                fontSize = 18.sp,
                                fontWeight = FontWeight.Bold,
                                color = MaterialTheme.colors.primary
                            )

                            IconButton(onClick = { currentYear++ }) {
                                Icon(Icons.Default.ArrowForward, contentDescription = "下一年")
                            }
                        }

                        Spacer(modifier = Modifier.height(16.dp))

                        // 月份网格
                        LazyVerticalGrid(
                            columns = GridCells.Fixed(3),
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.spacedBy(8.dp),
                            verticalArrangement = Arrangement.spacedBy(8.dp)
                        ) {
                            items(monthNames.indices.toList()) { index ->
                                val monthNumber = index + 1
                                val isSelected = monthNumber == currentMonth

                                Box(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .aspectRatio(1.5f)
                                        .clip(RoundedCornerShape(4.dp))
                                        .background(
                                            if (isSelected) MaterialTheme.colors.primary
                                            else MaterialTheme.colors.surface
                                        )
                                        .border(
                                            width = 1.dp,
                                            color = if (isSelected) MaterialTheme.colors.primary
                                            else MaterialTheme.colors.onSurface.copy(alpha = 0.2f),
                                            shape = RoundedCornerShape(4.dp)
                                        )
                                        .clickable {
                                            currentMonth = monthNumber
                                        },
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(
                                        text = monthNames[index],
                                        color = if (isSelected) MaterialTheme.colors.onPrimary
                                        else MaterialTheme.colors.onSurface,
                                        fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal
                                    )
                                }
                            }
                        }

                        Spacer(modifier = Modifier.height(16.dp))

                        // 按钮
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.End
                        ) {
                            TextButton(onClick = { showPicker = false }) {
                                Text("取消")
                            }

                            Spacer(modifier = Modifier.width(8.dp))

                            Button(onClick = {
                                // 创建新的日期，固定为每月1日
                                val newDate = LocalDate(currentYear, currentMonth, 1)
                                onDateSelected(newDate)
                                showPicker = false
                            }) {
                                Text("确认")
                            }
                        }
                    }
                }
            }
        }
    }
}